package com.gdth.bank.widget;

import com.gdth.bank.R;
import com.gdth.utils.ImageUtil;
import com.gdth.widget.dwonloadlist.DrawDownListAdapter;
import com.gdth.widget.dwonloadlist.DrawDownListTextView;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class BankDrawDownListTextView extends DrawDownListTextView{
	
	public TextView mEditText;
	/**
	 * 弹出框
	 */
	private PopupWindow pop;
	
	/**
	 * 下拉列表
	 */
	private ListView listView;
	/**
	 * 数据源适配器
	 */
	private DrawDownListAdapter popAdapter;  
	private Drawable drawable_up;
	private Drawable drawable_down;
	
	 int listView_Theme;
	    public void setTheme(int listView_Theme) {
	    	this.listView_Theme = listView_Theme;
	    }
	
	public BankDrawDownListTextView(Context context) {
		super(context, R.layout.widget_drawdownlist);
	}
	
	public BankDrawDownListTextView(Context context, AttributeSet attrs) {
		super(context, attrs, R.layout.widget_drawdownlist);
	}
	
	@Override
	public void initView(){
        txt_label = (TextView) findViewById(R.id.drawablelist_label);
        txt_value = (TextView) findViewById(R.id.drawablelist_edit);
        
        drawable_up = ImageUtil.getImageAsDrawable(context, R.drawable.drawdownlist_btn_up);
		drawable_down = ImageUtil.getImageAsDrawable(context, R.drawable.drawdownlist_btn_down);
        txt_value.setOnClickListener(new clickSelectListener());
        
        mEditText = txt_value;
        
        this.setTheme(R.drawable.background_corner_list);
	}
	
	private class clickSelectListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			setUpIbtnListeners();
		}
		
	}
	
	public Object getkey() {
		return this.getKey();
	}
	  /**
     * 生成弹出框
     */
	public void setUpIbtnListeners() {
		if (pop == null) { 
			
				popAdapter = new DrawDownListAdapter(this.context, this.items, touchListener);  
	
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(20,20,20,20);
				listView = new ListView(this.context);
				if (listView_Theme != 0) {
					listView.setBackgroundResource(listView_Theme);
				} else {
					listView.setBackgroundResource(R.drawable.background_corner_padding);
				}
				
				listView.setLayoutParams(layoutParams);
				
				listView.setAdapter(popAdapter); 
				
				newPop();
				
				txt_value.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable_up, null);
		//}else if (!popAdapter.isShow) {  
		}else if (!pop.isShowing()) {  
			popAdapter = new DrawDownListAdapter(this.context, this.items, touchListener);  
			
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(20,20,20,20);
			listView = new ListView(this.context);
			if (listView_Theme != 0) {
				listView.setBackgroundResource(listView_Theme);
			} else {
				listView.setBackgroundResource(R.drawable.background_corner_padding);
			}
			
			listView.setLayoutParams(layoutParams);
			
			listView.setAdapter(popAdapter); 
			
			newPop();
			
			txt_value.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable_up, null);
		} else if (popAdapter.isShow) {
			popupWindowHide();
		}
		
	}
	/**
	 * 列表点击事件
	 */
	OnClickListener touchListener = new OnClickListener() {  
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			TextView text = (TextView)arg0;
			
			key = text.getTag().toString();
			val = text.getText().toString();
            
			txt_value.setText(val);
			txt_value.setTag(key);
            
			if (selectListener != null) {
				selectListener.onchange(key, val);
			}
			
			popupWindowHide();
		}  
    };
    
    private void popupWindowHide(){
    	popAdapter.isShow = false; 
        pop.dismiss(); 
        
		txt_value.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable_down, null);
    }
    
    /**
     * 创建pop
     */
    private void newPop(){
    	pop = new PopupWindow(listView, this.getWidth(), LayoutParams.WRAP_CONTENT);  
		pop.setFocusable(true);
		pop.setTouchInterceptor(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(pop.isShowing()){
					txt_value.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable_down, null);
				}else{
					txt_value.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable_up, null);
				}
				
				return false;
			}
		});
		
		pop.showAsDropDown(this);  
		popAdapter.isShow = true;
		
    }
}
